package com.example.monitorapp.fragment


import android.text.Editable
import android.text.TextWatcher
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import androidx.fragment.app.Fragment
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import android.widget.Toast
import androidx.lifecycle.Observer
import androidx.navigation.Navigation
import com.example.baselib.constants.MyConstants
import com.example.baselib.dialog.MsgDialog
import com.example.baselib.fragment.BaseFragment

import com.example.monitorapp.R
import com.example.monitorapp.viewmodel.UserInfoViewModel
import kotlinx.android.synthetic.main.fragment_bind_tel.*
import kotlinx.android.synthetic.main.fragment_change_pwd.*

/**
 * A simple [Fragment] subclass.
 */
class ChangePwdFragment : BaseFragment<UserInfoViewModel>() {

    override fun providerVMClass(): Class<UserInfoViewModel>? {
        return UserInfoViewModel::class.java
    }

    override fun contentLayout(): Int {
        return R.layout.fragment_change_pwd
    }

    override fun initView() {
        change_old_edt.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
                isButtonEnable()
                change_odl_img?.visibility =
                    if (s?.length!! > 0) View.VISIBLE else View.INVISIBLE
                change_osh_img?.visibility =
                    if (s?.length!! > 0) View.VISIBLE else View.INVISIBLE
                change_old_edt.background =
                    if (s?.length!! > 0) resources.getDrawable(R.drawable.change_ed_bg_1, null)
                    else resources.getDrawable(R.drawable.change_ed_bg, null)
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }
        })
        change_new_edt.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
                isButtonEnable()
                change_ndl_img?.visibility =
                    if (s?.length!! > 0) View.VISIBLE else View.INVISIBLE
                change_nsh_img?.visibility =
                    if (s?.length!! > 0) View.VISIBLE else View.INVISIBLE
                change_new_edt.background =
                    if (s?.length!! > 0) resources.getDrawable(R.drawable.change_ed_bg_1, null)
                    else resources.getDrawable(R.drawable.change_ed_bg, null)
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }
        })
        change_sure_edt.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
                isButtonEnable()
                change_sdl_img?.visibility =
                    if (s?.length!! > 0) View.VISIBLE else View.INVISIBLE
                change_ssh_img?.visibility =
                    if (s?.length!! > 0) View.VISIBLE else View.INVISIBLE
                change_sure_edt.background =
                    if (s?.length!! > 0) resources.getDrawable(R.drawable.change_ed_bg_1, null)
                    else resources.getDrawable(R.drawable.change_ed_bg, null)
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }
        })
        change_odl_img.setOnClickListener { edClear(change_old_edt) }
        change_ndl_img.setOnClickListener { edClear(change_new_edt) }
        change_sdl_img.setOnClickListener { edClear(change_sure_edt) }
        change_osh_img.setOnClickListener { edShowHide(change_osh_img, change_old_edt) }
        change_nsh_img.setOnClickListener { edShowHide(change_nsh_img, change_new_edt) }
        change_ssh_img.setOnClickListener { edShowHide(change_ssh_img, change_sure_edt) }
        change_sure_btn.setOnClickListener { changePwd() }

    }

    override fun initData() {
        viewModel.getResult().observe(this, Observer {
            MsgDialog(it.msg).setCallbackListener(object : MsgDialog.onDismissCallback{
                override fun dismiss() {
                    activity?.onBackPressed()
                }
            }).show(activity!!.supportFragmentManager,"getResult")
        })
    }


    /**
     * 控制button
     */
    private fun isButtonEnable() {
        change_sure_btn.isEnabled =
            !(change_old_edt.text.isNullOrEmpty() || change_new_edt.text.isNullOrEmpty() || change_sure_edt.text.isNullOrEmpty())
    }

    private fun edClear(view: EditText) {
        view.text.clear()
        view.requestFocus()
    }

    private fun edShowHide(img: ImageView, ed: EditText) {
        if (ed.transformationMethod != HideReturnsTransformationMethod.getInstance()) {// 显示密码
            img?.setImageResource(R.mipmap.change_pwd_show)
            ed.transformationMethod = HideReturnsTransformationMethod.getInstance()
            ed.setSelection(ed.text.length)
        } else {// 隐藏密码
            img?.setImageResource(R.mipmap.change_pwd_hide)
            ed.transformationMethod = PasswordTransformationMethod.getInstance()
            ed.setSelection(ed.text.length)
        }
        ed.requestFocus()
    }

    private fun changePwd() {
        val oldPwd=change_old_edt.text.toString()
        val newPwd=change_new_edt.text.toString()
        val surePwd=change_sure_edt.text.toString()
        if (newPwd == surePwd){
            viewModel.changePwd(oldPwd,newPwd,surePwd)
        }
        else{
            Toast.makeText(activity,"两次密码不一致",Toast.LENGTH_SHORT).show()
        }
    }
}
